package pl.mcnova.model.table;

import java.util.List;

import pl.mcnova.model.consts.ContractorConsts;
import pl.mcnova.model.dao.DeviceDAO;
import pl.mcnova.model.dao.PhoneNumberDAO;
import pl.mcnova.model.entity.Contractor;
import pl.mcnova.model.entity.Device;
import pl.mcnova.model.entity.PhoneNumber;

public class ContractorTableModel extends TableModel<Contractor> {

	private PhoneNumberDAO phoneNumberDAO;
	private DeviceDAO deviceDAO;

	public ContractorTableModel(List<Contractor> contractors) {
		super(contractors);
		phoneNumberDAO = new PhoneNumberDAO();
		deviceDAO = new DeviceDAO();

	}

	public ContractorTableModel() {
		super();
		phoneNumberDAO = new PhoneNumberDAO();
		deviceDAO = new DeviceDAO();

	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (rowIndex > -1 && rowIndex < data.size() && columnIndex > -1 && columnIndex < classNames.length) {

			if (data.get(rowIndex) == null) {
				if (columnIndex == 0)
					return rowIndex + 1;
				else
					return null;
			}

			Contractor c = data.get(rowIndex);

			switch (columnIndex) {
			case 0:
				return c.getId();
			case 1:
				return c.getName();
			case 2:
				return c.getStreet();
			case 3:
				return c.getHouseNumber();
			case 4:
				return c.getPostalCode();
			case 5:
				return c.getCity();
			case 6:
				return c.getNip();
			case 7:
				return c.getEmail();
			case 8:
				return c.getWww();
			case 9:
				return c.getStatus();
			case 10:
				List<PhoneNumber> list = c.getPhones();
				if (list != null && list.size() > 0) return list.get(0);
				return null;
			case 11:
				List<Device> devices = deviceDAO.getAllfrom(c.getId());
				if (devices != null && devices.size() > 0) return devices.get(0);
				return "";
			default:
				return "";

			}
		} else {
			return null;
		}
	}

	@Override
	public void setColumns() {

		columnNames = new String[] { "Id", ContractorConsts.name, ContractorConsts.street,
				ContractorConsts.houseNumber, ContractorConsts.postalCode, ContractorConsts.city, ContractorConsts.nip,
				ContractorConsts.email, ContractorConsts.www, ContractorConsts.status, ContractorConsts.phone,
				ContractorConsts.device };

		classNames = new Class[] { Integer.class, String.class, String.class, String.class, String.class, String.class,
				String.class, String.class, String.class, String.class, String.class, String.class };

	}

	public int getIndex(Integer id) {
		for (int i = 0; i < getRowCount(); i++) {
			if (id.equals(data.get(i).getId())) return i;

		}
		return -1;
	}

}
